package com.example.product.config;

import com.example.product.exception.CustomException;
import com.example.product.util.Result;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Log log = LogFactory.get();

    @ExceptionHandler(Exception.class)
    public Result allException(Exception e) {
        e.printStackTrace();
        log.error("异常信息：", e);
        return new Result(501, "全局异常", e.getMessage());
    }

    @ExceptionHandler(CustomException.class)
    public Result customError(HttpServletRequest request, CustomException e) {
        return Result.error(e.getMsg());
    }
}